<template>
  <el-row type="flex" justify="space-between" align="middle" style="width: 100%; height: 40px;">
    <!-- 左边 -->
    <el-col>{{ treeNode.name }}</el-col>
    <!-- /左边 -->
    <!-- 右边 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
    <!-- /右边 -->
  </el-row>
</template>

<script>
import { delDepartmentList } from '@/api/departments'
export default {
  props: {
    treeNode: {
      required: true, // 设置当前数据为必填
      type: Object // 类型是Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleCommand(type) {
      if (type === 'add') {
        // 添加部门
        // 打开对话框
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑部门
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除部门
        try {
          await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          try {
            await delDepartmentList(this.treeNode.id)
            this.$message.success('删除成功')
            this.$emit('delDepts')
          } catch (error) {
            console.log(error.message, '删除失败')
          }
        } catch (error) {
          console.log(error, '取消删除')
        }
      }
    }
  }
}
</script>

<style>

</style>
